/**
 *
 */
package de.cbf.cam.dsa.entity.edit.item.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import de.cbf.cam.dsa.entity.edit.item.act.ActivatorEditEntityItem;
import de.cbf.cam.dsa.entity.edit.item.persistence.ControllerPortXMLItem;
import de.cbf.cam.dsa.entity.edit.item.views.ItemFacetsView;
import de.cbf.cam.dsa.entity.edit.talent.resolver.service_resolver.IServiceResolver_EntityTalent;
import de.cbf.cam.dsa.facet.armor.FacetArmor;
import de.cbf.cam.dsa.facet.container.FacetContainer;
import de.cbf.cam.dsa.facet.garment.FacetGarment;
import de.cbf.cam.dsa.facet.item.FacetItem;
import de.cbf.cam.dsa.facet.weapon_cc.FacetWeaponCC;
import de.cbf.cam.dsa.facet.weapon_cc.editor.ControllerEditorFacetWeaponCC;
import de.cbf.cam.dsa.facet.weapon_ranged.FacetWeaponRanged;
import de.cbf.cam.dsa.facet.weapon_ranged.editor.ControllerEditorFacetWeaponRanged;
import de.cbf.util.entity.edit.controller.customizing.IFacetControllerCustomizer;
import de.cbf.util.entity.edit.port.AbstractEditEntityMainControllerPort;
import de.cbf.util.entity.edit.service.IServiceFacetEditController;
import de.cbf.util.entity.facet.edit.controller.AbstractControllerEditFacet;
import de.cbf.util.entity.facet.interfaces.IFacetOfEntity;
import de.cbf.util.entity.facet.persistence.controller.AbstractControllerPersistEntityViaResolver;
import de.cbf.util.entity.facet.persistence.resolver.IServiceResolverEntity;
import de.cbf.util.entity.facet.persistence.xml.controller.AbstractControllerPortXMLFacet;

/**
 * @author Nebelritter
 * TODO make facet for alchemist stuff with "base item" for Bottle
 * TODO make facet for enchanted items with spells that are bound (easier to analyze later), import from "Artefakt Generator"
 * TODO make facet for tool-for-talent to specify if an item can be used as tool for a talent check and with bonus, 
 * use talentlist, for tool for multiple talents
 */
public class EditItemsMainController extends AbstractEditEntityMainControllerPort {
	private static final Log logger = LogFactory.getLog(EditItemsMainController.class);

	protected static EditItemsMainController instance;

	protected ControllerPortXMLItem imExPortController;
	protected AbstractControllerPersistEntityViaResolver persistenceController;
	/**
	 *
	 */
	private EditItemsMainController() {
		logger.trace("creating: "+EditItemsMainController.class.getSimpleName());
	}
	
	/**
	 * @return the instance
	 */
	public static EditItemsMainController getInstance() {
		if(instance == null){
			instance = new EditItemsMainController();
		}
		return instance;
	}	

	@Override
	protected Class<? extends IFacetOfEntity> getActiveBaseFacet() {		
		return FacetItem.class;
	}

	@Override
	protected String getViewID() {		
		return ItemFacetsView.ID;
	}

	@Override
	protected String getLogViewSecondaryID() {		
		return  ActivatorEditEntityItem.LOG_VIEW_ITEM_EDIT_SEC_ID;
	}

	@Override
	protected List<Class<? extends IFacetOfEntity>> getFacetClasses() {		
		List<Class<? extends IFacetOfEntity>> resultList = new ArrayList<Class<? extends IFacetOfEntity>>();
		resultList.add(FacetItem.class);
		resultList.add(FacetContainer.class);
		resultList.add(FacetGarment.class);
		resultList.add(FacetArmor.class);
		resultList.add(FacetWeaponCC.class);
		resultList.add(FacetWeaponRanged.class);
		return resultList ;
	}

	@Override
	protected IServiceFacetEditController getServiceFacetEditController() {
		return ActivatorEditEntityItem.getDefault().getServiceFacetEditController();
	}
	
	@Override
	protected IServiceResolverEntity getResolverForEntity() {
		IServiceResolverEntity service = ActivatorEditEntityItem.getDefault().getServiceResolverEntity();
		if(service != null){			
			return service;
		}else{
			logger.warn("No resolver service found for EntityTalent, will not be able to persist/resolve");
			return null;
		}		
	}

	@Override
	protected AbstractControllerPortXMLFacet getImportController() {
		if(imExPortController == null){
			imExPortController = new ControllerPortXMLItem();
		}
		return imExPortController;
	}

	@Override
	protected AbstractControllerPortXMLFacet getExportController() {
		if(imExPortController == null){
			imExPortController = new ControllerPortXMLItem();
		}
		return imExPortController;
	}
	
	protected Map<Class<?>, IFacetControllerCustomizer> generateControllerCustomizerConfig() {
		//generate customizer for facetController(s) to provide entities for choice of talents
		Map<Class<?>, IFacetControllerCustomizer> result = new HashMap<Class<?>, IFacetControllerCustomizer>();
		
		final IServiceResolver_EntityTalent serviceResolverTalents = ActivatorEditEntityItem.getDefault()
																		.getServiceResolverEntityTalent();
		
		IFacetControllerCustomizer customizerTalents = new IFacetControllerCustomizer() {
			//this will customize talent edit controller to set all talent entities from resolver, to make choice of 
			//compensation talent possible
			@Override			
			public boolean customizeFacetController(AbstractControllerEditFacet<?> controller) {
				if (controller instanceof ControllerEditorFacetWeaponCC) {
					ControllerEditorFacetWeaponCC editorFacetWeaponCC = (ControllerEditorFacetWeaponCC) controller;
					editorFacetWeaponCC.setTalentResolver(serviceResolverTalents);
					return true;
				}				
				if (controller instanceof ControllerEditorFacetWeaponRanged) {
					ControllerEditorFacetWeaponRanged editorFacetWeaponRanged = 
							(ControllerEditorFacetWeaponRanged) controller;
					editorFacetWeaponRanged.setTalentResolver(serviceResolverTalents);
					return true;
				}
				return false;
			}
		};
		result.put(FacetWeaponCC.class		, customizerTalents);
		result.put(FacetWeaponRanged.class	, customizerTalents);
		return result;
	}
}
